package cn.unknowpupil.init.domain.gen;

import cn.unknowpupil.init.common.constant.GenConstants;
import cn.unknowpupil.init.utils.StringUtils;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ArrayUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created at 2020-11-30 by  w10g <br>
 * Initialize GenTable  ... <br>
 * 代码生成业务表
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("gen_table")
@ApiModel(value = "GenTable 域", description = "代码生成业务表")
public class GenTable extends Model<GenTable> {

    private static final long serialVersionUID = 1L;
    /**
     * 编号
     */
    @TableId(value = "table_id", type = IdType.AUTO)
    @ApiModelProperty(value = "编号")

    @TableField(value = "table_id")
    private Long tableId;
    /**
     * 表名称
     */
    @ApiModelProperty(value = "表名称")
    @TableField(value = "table_name")
    private String tableName;
    /**
     * 表描述
     */
    @ApiModelProperty(value = "表描述")
    @TableField(value = "table_comment")
    private String tableComment;
    /**
     * 实体类名称
     */
    @ApiModelProperty(value = "实体类名称")
    @TableField(value = "class_name")
    private String className;
    /**
     * 使用的模板（crud单表操作 tree树表操作）
     */
    @ApiModelProperty(value = "使用的模板（crud单表操作 tree树表操作）")
    @TableField(value = "tpl_category")
    private String tplCategory;
    /**
     * 生成包路径
     */
    @ApiModelProperty(value = "生成包路径")
    @TableField(value = "package_name")
    private String packageName;
    /**
     * 生成模块名
     */
    @ApiModelProperty(value = "生成模块名")
    @TableField(value = "module_name")
    private String moduleName;
    /**
     * 生成业务名
     */
    @ApiModelProperty(value = "生成业务名")
    @TableField(value = "business_name")
    private String businessName;
    /**
     * 生成功能名
     */
    @ApiModelProperty(value = "生成功能名")
    @TableField(value = "function_name")
    private String functionName;
    /**
     * 生成功能作者
     */
    @ApiModelProperty(value = "生成功能作者")
    @TableField(value = "function_author")
    private String functionAuthor;
    /**
     * 生成代码方式（0zip压缩包 1自定义路径）
     */
    @ApiModelProperty(value = "生成代码方式（0zip压缩包 1自定义路径）")
    @TableField(value = "gen_type")
    private String genType;
    /**
     * 生成路径（不填默认项目路径）
     */
    @ApiModelProperty(value = "生成路径（不填默认项目路径）")
    @TableField(value = "gen_path")
    private String genPath;
    /**
     * 其它生成选项
     */
    @ApiModelProperty(value = "其它生成选项")
    @TableField(value = "options")
    private String options;
    /**
     * 创建者
     */
    @ApiModelProperty(value = "创建者")
    @TableField(value = "create_by")

    private String createBy;
    /**
     * 创建时间
     */
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建时间")
    @TableField(value = "create_time")
    private Date createTime;
    /**
     * 更新者
     */
    @ApiModelProperty(value = "更新者")
    @TableField(value = "update_by")
    private String updateBy;
    /**
     * 更新时间
     */
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "更新时间")
    @TableField(value = "update_time")
    private Date updateTime;
    /**
     * 备注
     */
    @ApiModelProperty(value = "备注")
    @TableField(value = "remark")
    private String remark;
    /**
     * 开始时间
     */
    @TableField(exist = false)
    @JsonIgnore
    private String beginTime;

    /**
     * 结束时间
     */
    @TableField(exist = false)
    @JsonIgnore
    private String endTime;
    /**
     * 主键信息
     */
    @TableField(exist = false)
    private GenTableColumn pkColumn;
    @TableField(exist = false)
    /** 请求参数 */
    private Map<String, Object> params;


    /**
     * 树编码字段
     */
    @TableField(exist = false)
    private String treeCode;


    /**
     * 树父编码字段
     */
    @TableField(exist = false)
    private String treeParentCode;

    /**
     * 树名称字段
     */
    @TableField(exist = false)
    private String treeName;

    /**
     * 上级菜单ID字段
     */
    @TableField(exist = false)
    private String parentMenuId;

    /**
     * 上级菜单名称字段
     */
    @TableField(exist = false)
    private String parentMenuName;


    /**
     * 表列信息
     */
    @TableField(exist = false)
    private List<GenTableColumn> columns;

    public GenTableColumn getPkColumn() {
        return pkColumn;
    }

    public List<GenTableColumn> getColumns() {
        return columns;
    }


    public Map<String, Object> getParams() {
        if (params == null) {
            params = new HashMap<>();
        }
        return params;
    }


    public void setTreeParentCode(String treeParentCode) {
        this.treeParentCode = treeParentCode;
    }

    public boolean isSuperColumn(String javaField) {
        return isSuperColumn(this.tplCategory, javaField);
    }

    public static boolean isSuperColumn(String tplCategory, String javaField) {
        if (isTree(tplCategory)) {
            return StringUtils.equalsAnyIgnoreCase(javaField,
                    ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
        }
        return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
    }

    public static boolean isTree(String tplCategory) {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREE, tplCategory);
    }


    public boolean isTree() {
        return isTree(this.tplCategory);
    }

    public boolean isCrud() {
        return isCrud(this.tplCategory);
    }

    public static boolean isCrud(String tplCategory) {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory);
    }
}